﻿
using StackExchange.Redis;

namespace RBAC_Uthorization.Read.API.Extensions
{
    public class RedisConnectionService
    {
        private readonly IConnectionMultiplexer _connectionMultiplexer;
        private readonly IConfiguration _configuration;

        public RedisConnectionService(IConfiguration configuration)
        {
            _configuration = configuration;
            if (_configuration["Redis:Redisif"] == "1")
            {
                _connectionMultiplexer = ConnectionMultiplexer.Connect(_configuration["Redis:ConnectionString"]);
            }
        }

        public async Task<bool> SetAsync(string key, string value)
        {
            if (_configuration["Redis:Redisif"] == "1" && _connectionMultiplexer != null)
            {
                var db = _connectionMultiplexer.GetDatabase();
                return await db.StringSetAsync(key, value);
            }
            return false;
        }

        public async Task<bool> DeleteAsync(string key)
        {
            if (_configuration["Redis:Redisif"] == "1" && _connectionMultiplexer != null)
            {
                var db = _connectionMultiplexer.GetDatabase();
                return await db.KeyDeleteAsync(key);
            }
            return false;
        }

        public async Task<string> GetAsync(string key)
        {
            if (_configuration["Redis:Redisif"] == "1" && _connectionMultiplexer != null)
            {
                var db = _connectionMultiplexer.GetDatabase();
                return await db.StringGetAsync(key);
            }
            return null;
        }
    }
}

